package org.tc.service.impl;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.client.HttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.tc.bean.Item;
import org.tc.bean.ItemDesc;
import org.tc.common.Result;
import org.tc.controller.ItemController;
import org.tc.exception.CustomException;
import org.tc.service.ItemRpcService;
import org.tc.service.ItemService;
import org.tc.service.JedisService;
import org.tc.utils.HttpClientUtil;
import org.tc.utils.JsonUtils;

import java.io.IOException;

/**
 * Created by zxd on 2018/1/29.
 */
@Service
public class ItemServiceImpl implements ItemService{

    private Logger LOGGER= LoggerFactory.getLogger(ItemService.class);

    @Autowired
    private JedisService jedisService;

    public static final  String ITEM_URL="http://manage.yufan.com/rest/api/item/";

    public static final  String ITEM_DESC_URL="http://manage.yufan.com/rest/api/item/desc/";

    public static final String ITEM_CACHE="item_cache:";


    @Autowired
    private ItemRpcService itemRpcService;

    //通过dubbo
    @Override
    public Item queryItemById(Long itemId) throws CustomException {
        //后台系统返回的json数据
        //1.首先从redis查询数据，如果没有找到在请求后台系统
        String json=jedisService.get(ITEM_CACHE+itemId);
        Item item=null;

        if(json==null){
            LOGGER.info("缓存中没有获取到数据 商品id为:{}",itemId);

            //通过HTTP调用
           // json = HttpClientUtil.doGet(ITEM_URL + itemId);
            item = itemRpcService.queryItemByItemId(itemId);

            if(item==null){
                LOGGER.info("请求后台数据失败，没有查询到数据 {}",itemId);
                return null;
            }
            json=JsonUtils.objectToJson(item);

            //2.请求到后要把数据放入缓存
            jedisService.set(ITEM_CACHE+itemId,json);
            jedisService.expire(ITEM_CACHE+itemId,60*60*2);
             LOGGER.info("向缓存中设置数据key为 {}.",ITEM_CACHE+itemId);
        }else{
            LOGGER.info("从缓存中获取数据 itemId{}.",itemId);
        }
        return JsonUtils.jsonToPojo(json,Item.class);
    }

    //通过http
    @Override
    public ItemDesc queryItemDescById(Long itemId) throws CustomException {
        //后台系统返回的json数据
        String json = HttpClientUtil.doGet(ITEM_DESC_URL + itemId);
        LOGGER.debug("请求的后台接口地址是:{}.",ITEM_DESC_URL+itemId);
        if(StringUtils.isEmpty(json)){
            LOGGER.info("商品id {}没有查找到数据",itemId);
            return null;
        }
        LOGGER.debug("商品id为{},请求的后台数据是{}",itemId,json);
        ObjectMapper objectMapper=new ObjectMapper();
        try {
            //把获取到的json数据转换成node节点，然后获取data的数据
            JsonNode jsonNode = objectMapper.readTree(json);
            JsonNode data = jsonNode.get("data");
            String itemDesc_data=data.toString();
            LOGGER.debug("商品描述的数据为{}",itemDesc_data);
            return JsonUtils.jsonToPojo(itemDesc_data,ItemDesc.class);
        } catch (IOException e) {
            e.printStackTrace();
            LOGGER.error("商品描述查询请求失败商品id为:{}",itemId);
            throw  new CustomException("商品描述查询请求失败商品id为:" +itemId);
        }
    }
}
